﻿

#region Using Statements

// System references.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// XNA references.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

// Freeform references.
using Freeform.Framework;
using Freeform.Framework.Graphics;

#endregion


namespace CardGame.Card
{
    /// <summary>
    /// 
    /// </summary>
    public class Card : Entity
    {

        #region Members

        public RenderTarget2D RenderTarget { get; private set; }


        public SpriteBatch Batch { get; private set; }


        public Texture2D Trim { get; set; }


        public Color TrimColour { get; set; }


        private Vector2 Origin { get; set; }


        public Single Depth { get; set; }


        public Texture2D Art { get; set; }


        public Texture2D Fill { get; set; }


        public Color FillColour { get; set; }


        public String Name { get; set; }


        public SpriteFont Font { get; set; }


        public Color FontColour { get; set; }


        public String Type { get; set; }


        public Color ColourOverlay { get; set; }


        public Color DullColour { get; set; }


        public Color BrightColour { get; set; }


        private Style _Style = Style.Forest;
        public Style Style { get { return _Style; } set { UpdateStyle(value); } }

        private Edition _Edition = Edition.Standard;
        public Edition Edition { get { return _Edition; } set { UpdateEdition(value); } }


        public Boolean NeedsReRender { get; set; }

        #endregion


        #region Init

        public Card(Board Board, String Name, String Type, Style Style, Edition Edition) : base(Board)
        {
            this.Style = Style;
            this.Edition = Edition;
            this.Name = Name;
            this.Type = Type;

            Trim = Game.Content.Load<Texture2D>("Texture/Card/Common/Trim (Textured)");
            Art = Game.Content.Load<Texture2D>("Texture/Card/Art/" + Name);
            Fill = Game.Content.Load<Texture2D>("Texture/Card/Common/Fill (Textured)");
            Font = Game.Content.Load<SpriteFont>("Font/Medieval");
            
            BrightColour = Color.White;
            DullColour = new Color(0.7f, 0.7f, 0.7f);
            RenderTarget = new RenderTarget2D(Game.GraphicsDevice, 256, 384);
            Origin = new Vector2(RenderTarget.Width, RenderTarget.Height) / 2;
            Batch = new SpriteBatch(Game.GraphicsDevice);
            SpatialProperties.Size = new Vector2(256, 384);
            ReRender();
        }

        #endregion


        #region Update

        protected override void DoUpdate(GameTime Time)
        {
            base.DoUpdate(Time);

            if (IsMouseOver())
            {
                ColourOverlay = BrightColour;
                SpatialProperties.Size = new Vector2(256, 384) * 1.3f;
            }
            else
            {
                ColourOverlay = DullColour;
                SpatialProperties.Size = new Vector2(256, 384);
            }


        }

        #endregion


        #region Draw

        protected override void DoDraw(GameTime Time, Viewport2D Viewport)
        {
            base.DoDraw(Time, Viewport);

            if (NeedsReRender)
                ReRender();

            // Calculate drawing data.
            Vector2 Draw_Position = Viewport.Camera.TransformPosition(SpatialProperties.Position);
            Vector2 Draw_Size = Viewport.Camera.TransformSize(SpatialProperties.Size);
            Rectangle Draw_Destination = new Rectangle((int)Draw_Position.X, (int)Draw_Position.Y, (int)Draw_Size.X, (int)Draw_Size.Y);

            // Draw entity.
            Viewport.SpriteBatch.Draw(RenderTarget, Draw_Destination, null, ColourOverlay, SpatialProperties.Direction, Origin, SpriteEffects.None, Depth);
        }


        public virtual void ReRender()
        {
            Game.GraphicsDevice.SetRenderTarget(RenderTarget);
            Game.GraphicsDevice.Clear(Color.Transparent);
            Batch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            Batch.Draw(Trim, new Rectangle(0, 0, 256, 384), TrimColour);
            Batch.Draw(Art, new Rectangle(18, 27, 220, 189), Color.White);
            Batch.Draw(Fill, new Rectangle(8, 10, 240, 368), FillColour);
            Batch.DrawString(Font, Name, new Vector2(18, 12), FontColour);
            Vector2 Measure = Font.MeasureString(Type);
            Batch.DrawString(Font, Type, new Vector2(236 - Measure.X, 217), FontColour);
            Batch.End();
            NeedsReRender = false;
        }

        #endregion

        private void UpdateStyle(Style Sty)
        {
            _Style = Sty;
            switch (Style)
            {
                case Style.Forest: FillColour = Color.Green; FontColour = Color.White; break;
                case Style.Island: FillColour = Color.CornflowerBlue; FontColour = Color.White; break;
                case Style.Mountain: FillColour = Color.Red; FontColour = Color.White; break;
                case Style.Swamp: FillColour = new Color(0.35f, 0.35f, 0.35f); FontColour = Color.White; break;
                case Style.Plains: FillColour = new Color(0.95f, 0.95f, 0.95f); FontColour = Color.Black; break;
            }
            NeedsReRender = true;
        }


        private void UpdateEdition(Edition Ed)
        {
            _Edition = Ed;
            switch (Edition)
            {
                case Edition.Standard: TrimColour = new Color(0.2f, 0.2f, 0.2f); break;
                case Edition.Special: TrimColour = Color.White; break;
            }
            NeedsReRender = true;
        }

    }
}
